#!/bin/bash

# 设置变量
REGISTRY="1.94.151.57:85"
NAMESPACE="auto-test"
BASE_IMAGE_NAME="fault-inject/kube-relayer-base"
INTERCEPT_IMAGE_NAME="fault-inject/kube-relayer-intercept"
TAG="latest"
HELM_RELEASE_NAME="kube-relayer"

# 显示帮助信息
show_help() {
    echo "KubeRelayer 部署脚本"
    echo ""
    echo "用法: $0 [选项]"
    echo ""
    echo "选项:"
    echo "  -h, --help                显示帮助信息"
    echo "  -b, --build               构建 Docker 镜像"
    echo "  -p, --push                推送 Docker 镜像到仓库"
    echo "  -d, --deploy              部署到 Kubernetes"
    echo "  -u, --upgrade             升级 Kubernetes 部署"
    echo "  -a, --all                 执行所有操作（构建、推送、部署）"
    echo "  -n, --namespace NAMESPACE 指定 Kubernetes 命名空间（默认: auto-test）"
    echo "  -t, --tag TAG             指定镜像标签（默认: latest）"
    echo ""
    echo "示例:"
    echo "  $0 --build                 # 仅构建镜像"
    echo "  $0 --push                  # 仅推送镜像"
    echo "  $0 --deploy                # 仅部署到 Kubernetes"
    echo "  $0 --all                   # 执行所有操作"
    echo "  $0 --all --tag v1.0.0      # 使用指定标签执行所有操作"
}

# 构建 Docker 镜像
build_images() {
    echo "构建 Docker 镜像..."

    echo "构建基础信息处理组件镜像..."
    docker build --network host -t ${REGISTRY}/${BASE_IMAGE_NAME}:${TAG} -f backend/dockerfiles/base/Dockerfile backend/

    echo "构建请求拦截组件镜像..."
    docker build --network host -t ${REGISTRY}/${INTERCEPT_IMAGE_NAME}:${TAG} -f backend/dockerfiles/intercept/Dockerfile backend/

    echo "镜像构建完成"
}

# 推送 Docker 镜像到仓库
push_images() {
    echo "推送 Docker 镜像到仓库..."

    echo "推送基础信息处理组件镜像..."
    docker push ${REGISTRY}/${BASE_IMAGE_NAME}:${TAG}

    echo "推送请求拦截组件镜像..."
    docker push ${REGISTRY}/${INTERCEPT_IMAGE_NAME}:${TAG}

    echo "镜像推送完成"
}

# 部署到 Kubernetes
deploy_to_k8s() {
    echo "部署到 Kubernetes..."

    # 检查命名空间是否存在，不存在则创建
    kubectl get namespace ${NAMESPACE} > /dev/null 2>&1 || kubectl create namespace ${NAMESPACE}

    # 使用 Helm 部署
    helm upgrade --install ${HELM_RELEASE_NAME} helm/kube-relayer \
        --namespace ${NAMESPACE} \
        --set base.image.tag=${TAG} \
        --set intercept.image.tag=${TAG}

    echo "部署完成"
}

# 升级 Kubernetes 部署
upgrade_k8s_deployment() {
    echo "升级 Kubernetes 部署..."

    # 使用 Helm 升级
    helm upgrade ${HELM_RELEASE_NAME} helm/kube-relayer \
        --namespace ${NAMESPACE} \
        --set base.image.tag=${TAG} \
        --set intercept.image.tag=${TAG}

    echo "升级完成"
}

# 解析命令行参数
BUILD=false
PUSH=false
DEPLOY=false
UPGRADE=false

while [[ $# -gt 0 ]]; do
    key="$1"
    case $key in
        -h|--help)
            show_help
            exit 0
            ;;
        -b|--build)
            BUILD=true
            shift
            ;;
        -p|--push)
            PUSH=true
            shift
            ;;
        -d|--deploy)
            DEPLOY=true
            shift
            ;;
        -u|--upgrade)
            UPGRADE=true
            shift
            ;;
        -a|--all)
            BUILD=true
            PUSH=true
            DEPLOY=true
            shift
            ;;
        -n|--namespace)
            NAMESPACE="$2"
            shift
            shift
            ;;
        -t|--tag)
            TAG="$2"
            shift
            shift
            ;;
        *)
            echo "未知选项: $1"
            show_help
            exit 1
            ;;
    esac
done

# 执行操作
if [ "$BUILD" = true ]; then
    build_images
fi

if [ "$PUSH" = true ]; then
    push_images
fi

if [ "$DEPLOY" = true ]; then
    deploy_to_k8s
fi

if [ "$UPGRADE" = true ]; then
    upgrade_k8s_deployment
fi

# 如果没有指定任何操作，显示帮助信息
if [ "$BUILD" = false ] && [ "$PUSH" = false ] && [ "$DEPLOY" = false ] && [ "$UPGRADE" = false ]; then
    show_help
    exit 1
fi

echo "操作完成"
